package com.sec.android.app.myfiles.widget.listview.pinchview;

import android.view.View;

/**
 * Created by jaebae on 2016-05-21.
 */
public class AnimationViewInfo {
    private static GridViewItemInfo baseFromView;
    private static GridViewItemInfo baseToView;
    private static float startX;
    private static float startY;
    private static float fromDeltaX;
    private static float fromDeltaY;
    private static float toDeltaX;
    private static float toDeltaY;

    private GridViewItemInfo mFromView;
    private GridViewItemInfo mToView;

    private float mFromX = -1;
    private float mFromY = -1;
    private float mFromWidth = -1;
    private float mFromHeight = -1;
    private float mToX = -1;
    private float mToY = -1;
    private float mToWidth = -1;
    private float mToHeight = -1;


    private View mMoveView;


    static void clearBase() {
        baseFromView = null;
        baseToView = null;
        startX = 0;
        startY = 0;
        fromDeltaX = 0;
        fromDeltaY = 0;
        toDeltaX = 0;
        toDeltaY = 0;
    }

    static AnimationViewInfo createNew(GridViewItemInfo fromView, GridViewItemInfo toView) {
        if (baseFromView == null && fromView != null && fromView.mView != null) {
            baseFromView = fromView;
        } else {
            fromDeltaX = getDeltaX(fromDeltaX, baseFromView, fromView);
            fromDeltaY = getDeltaY(fromDeltaY, baseFromView, fromView);
        }
        if (baseToView == null && toView != null && toView.mView != null) {
            baseToView = toView;
        } else {
            toDeltaX = getDeltaX(toDeltaX, baseToView, toView);
            toDeltaY = getDeltaY(toDeltaY, baseToView, toView);
        }

        return new AnimationViewInfo(fromView, toView);
    }

    static float getDeltaX(float delta, GridViewItemInfo baseVew, GridViewItemInfo viewInfo) {
        if (delta == 0 && viewInfo.mView != null) {
            if (baseVew.mCol != viewInfo.mCol) {
                int deltaCol = Math.abs(viewInfo.mCol - baseVew.mCol);
                float deltaX = Math.abs(viewInfo.mView.getX() - baseVew.mView.getX());
                if (deltaCol != 0 && deltaX != 0) {
                    delta = deltaX / deltaCol;
                    startX = (int) viewInfo.mView.getX() % (int) deltaX;
                }
            }
        }

        return delta;
    }

    static float getDeltaY(float delta, GridViewItemInfo baseVew, GridViewItemInfo viewInfo) {
        if (delta == 0 && viewInfo.mView != null) {
            if (baseVew.mRow != viewInfo.mRow) {
                int deltaRow = Math.abs(viewInfo.mRow - baseVew.mRow);
                float deltaY = Math.abs(viewInfo.mView.getY() - baseVew.mView.getY());
                if (deltaRow != 0 && deltaY != 0) {
                    delta = deltaY / deltaRow;
                    startY = (int) viewInfo.mView.getY() % (int) deltaY;
                }
            }
        }

        return delta;
    }

    public View getView() {
        return mMoveView;
    }

    public float getFromX() {
        if (mFromX == -1) {
            mFromX = getX(mFromView, baseFromView, fromDeltaX);
        }
        return mFromX;
    }

    public float getFromY() {
        if (mFromY == -1) {
            mFromY = getY(mFromView, baseFromView, fromDeltaY);
        }
        return mFromY;
    }

    public float getFromWidth() {
        if (mFromWidth == -1) {
            mFromWidth = baseFromView.mView.getWidth();
        }
        return mFromWidth;
    }

    public float getFromHeight() {
        if (mFromHeight == -1) {
            mFromHeight = baseFromView.mView.getHeight();
        }
        return mFromHeight;
    }

    public float getToX() {
        if (mToX == -1) {
            mToX = getX(mToView, baseToView, toDeltaX);
        }
        return mToX;
    }

    public float getToY() {
        if (mToY == -1) {
            mToY = getY(mToView, baseToView, toDeltaY);
        }
        return mToY;
    }


    public float getToWidth() {
        if (mToWidth == -1) {
            mToWidth = baseToView.mView.getWidth();
        }
        return mToWidth;
    }

    public float getToHeight() {
        if (mToHeight == -1) {
            mToHeight = baseToView.mView.getHeight();
        }
        return mToHeight;
    }

    private float getX(GridViewItemInfo viewInfo, GridViewItemInfo baseViewInfo, float deltaSize) {
        float ret;
        if (viewInfo.mView != null) {
            ret = viewInfo.mView.getX();
        } else {
            int delta = viewInfo.mCol - baseViewInfo.mCol;
            ret = startX + delta * deltaSize;
        }
        return ret;
    }

    private float getY(GridViewItemInfo viewInfo, GridViewItemInfo baseViewInfo, float deltaSize) {
        float ret;
        if (viewInfo.mView != null) {
            ret = viewInfo.mView.getY();
        } else {
            int delta = viewInfo.mRow - baseViewInfo.mRow;
            ret = baseViewInfo.mView.getY() + delta * deltaSize;
        }
        return ret;
    }


    AnimationViewInfo(GridViewItemInfo fromView, GridViewItemInfo toView) {
        mFromView = fromView;
        mToView = toView;

        if (mToView.mView != null) {
            mMoveView = mToView.mView;
        } else {
            mMoveView = mFromView.mView;
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        if (mMoveView != null) {
            sb.append(mMoveView.getVisibility());
        }
        sb.append('(').append(mFromView.mPosition).append(')');
        sb.append("[").append(mFromView.mRow).append(',').append(mFromView.mCol).append(']');
        sb.append("->");
        sb.append('(').append(mToView.mPosition).append(')');
        sb.append('[').append(mToView.mRow).append(',').append(mToView.mCol).append(']');
        sb.append(" X = ").append(getFromX()).append("->").append(getToX());
        sb.append(" Y = ").append(getFromY()).append("->").append(getToY());
        sb.append(" W = ").append(getFromWidth()).append("->").append(getToWidth());
        sb.append(" H = ").append(getFromHeight()).append("->").append(getToHeight());
        sb.append(" ( ");
        if (mFromView.mView != null) {
            sb.append(mFromView.mView.hashCode());
        }
        sb.append(") -> (");
        if (mToView.mView != null) {
            sb.append(mToView.mView.hashCode());
        }
        sb.append(")");

        return sb.toString();
    }
}
